package com.peas.platform.module.icims.contract.initial.service.impl;

import cn.hutool.core.date.DateTime;
import com.peas.platform.common.core.configuration.RequestContextUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.contract.initial.domain.ContractInitialProcessDomain;
import com.peas.platform.module.icims.contract.initial.dto.ContractInitialDto;
import com.peas.platform.module.icims.contract.initial.dto.ContractInitialProcessDto;
import com.peas.platform.module.icims.contract.initial.enums.ContractInitialState;
import com.peas.platform.module.icims.contract.initial.service.ContractInitialProcessService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class ContractInitialProcessServiceImpl extends BaseServiceImpl<ContractInitialProcessDto> implements ContractInitialProcessService {

    @Resource
    private ContractInitialProcessDomain contractInitialProcessDomain;

    @Override
    public BaseDomain<ContractInitialProcessDto, ? extends BasePo> getDomain() {
        return contractInitialProcessDomain;
    }

    @Transactional
    @Override
    public void saveContractInitialProcessDto(ContractInitialDto cid, ContractInitialState state) {
        // 如果有记录，则不添加
        ContractInitialProcessDto c = new ContractInitialProcessDto();
        c.setContractInitialId(cid.getId());
        c.setState(state.getKey());
        long count = this.count(c);
        if (count > 0) {
            return;
        }
        ContractInitialProcessDto dto = new ContractInitialProcessDto();
        dto.setContractInitialId(cid.getId());
        dto.setState(state.getKey());
        dto.setOperation(state.getValue());
        dto.setOperator(Long.valueOf(RequestContextUtil.getCurrentUserId()));
        dto.setOperateTime(DateTime.now());
        this.save(dto);
    }
}
